using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;

namespace Copperline.Escriba
{
	/// <summary>
	/// Provides static methods for convenient application setup and logging.
	/// </summary>
	public sealed class Logger
	{
		private Logger() {}

		/// <summary>
		/// Serves as the container for all loggers
		/// in the application
		/// </summary>
		private static LogManager logmgr = new LogManager();

		/// <summary>
		/// Adds a logger, which may be later referenced by name
		/// </summary>
		/// <param name="sKey">the name by which the logger can be referenced</param>
		/// <param name="logger">the logger to add</param>
		public static void AddLogger( string sKey, ILogger logger )
		{
			logmgr.AddLogger( sKey, logger );
		}

		public static void RemoveLogger( string sKey )
		{
			logmgr.RemoveLogger( sKey );
		}

		public static StringCollection Keys
		{
			get { return logmgr.Keys; }
		}

		public static ILogger GetLogger( string sKey )
		{
			return logmgr[ sKey ];
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.debug</c> severity.
		/// </summary>
		/// <param name="sMessage">the message to write.</param>
		public static void Debug( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.debug ) );
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.info</c> severity.
		/// </summary>
		/// <param name="sMessage">the message to write</param>
		public static void Info( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.info ) );
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.notice</c> severity.
		/// </summary>
		/// <param name="sMessage"></param>
		public static void Notice( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.notice ) );
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.warning</c> severity.
		/// </summary>
		/// <param name="sMessage">the message to write</param>
		public static void Warning( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.warning ) );
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.error</c> severity
		/// </summary>
		/// <param name="sMessage"></param>
		public static void Error( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.error ) );
		}

		/// <summary>
		/// Logs a string message with <c>ESeverity.fatal</c> severity
		/// </summary>
		/// <param name="sMessage">the message to write</param>
		public static void Fatal( string sMessage )
		{
			Write( new LogEntry( sMessage, ESeverity.fatal ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.debug</c> severity.
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Debug( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.debug ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.info</c> severity.
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Info( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.info ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.notice</c> severity.
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Notice( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.notice ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.warning</c> severity.
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Warning( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.warning ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.error</c> severity
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Error( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.error ) );
		}

		/// <summary>
		/// Logs an exception with <c>ESeverity.fatal</c> severity
		/// </summary>
		/// <param name="ex">the exception to write.</param>
		public static void Fatal( Exception ex )
		{
			Write( new LogEntry( ex.Message + "\n" + ex.StackTrace, ESeverity.fatal ) );
		}

		/// <summary>
		/// Logs a <c>LogEntry</c> instance
		/// <see cref="LogEntry"/>
		/// </summary>
		/// <param name="le"></param>
		public static void Write( LogEntry le )
		{
			logmgr.Write( le );
		}
	}
}
